﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using Luna.WPF.ApplicationFramework;
using Caliburn.Core.Metadata;
using System.ComponentModel;
using XWord.WPF.ApplicationFramework;
using System.Collections;
using System.Threading;
using XWord.Fundamental.Domain.Services;
using XWord.WPF.ApplicationFramework.Threads;
using System.Windows.Media;
using XWord.GUI.Presenters.Interface;
using XWord.Fundamental.Domain;
using XWord.Common;

namespace XWord.GUI.Presenters.Words
{
    public interface IBaseWordExplainPresenter : IWordExplainPresenter
    {
        Hashtable GetBaseWordExplain(string[] arr);
    }

    [Singleton("BaseWordExplainPresenter", typeof(IBaseWordExplainPresenter))]
    public class BaseWordExplainPresenter : WordExplainPresenter, IBaseWordExplainPresenter
    {
        public BaseWordExplainPresenter()
        {
            this.DisplayName = "基础英汉词典";
            this.Index = 0;
            this.BinaryFile =BinaryFiles.BaseWordDict;
            chars = new string[27];
            for (int i = 0; i <= 25; i++)
                chars[i] = ((char)(i + 65)).ToString().ToLower();
            chars[26] = "*";
        }
        string[] chars;
        public IWordBehavior WordBehavior { get; set; }

        protected override void OnInitialize()
        {
            WordBehavior.WordSearchEventHandler += new WordCancelEventHandler(WordBehavior_WordSearchEventHandler);
            base.OnInitialize();
        }

        void WordBehavior_WordSearchEventHandler(object sender, WordCancelEventArgs args)
        {
            foreach (var item in args.Word)
            {
                if (chars.Contains(item.ToString().ToLower()))
                {
                    return;
                }
            }
            
            lock (this)
            {
                //base.QueryData();
                WordExplorerPresenter.WordList = WordExplorerPresenter.EntityList
                    .Where(e => FilerChinese(args.Word, e.Value.Comment)).Select(e=>e.Key).ToObservableCollection();
                args.Cancel = true;
            }
        }
        private bool FilerChinese(string word, string ht)
        {
            if (ht == null) return false;
            return !string.IsNullOrEmpty(ht) && ht.Contains(word);
        }

        public IWordExplorerPresenter WordExplorerPresenter { get; set; }

        public override Hashtable TransFormWordToHashtable(Word word)
        {
            var ht = new Hashtable();
            ht["Pron"] = word.Pron;
            ht["Comment"] = word.Comment;
            return ht;
        }

        public Hashtable GetBaseWordExplain(string[] arr)
        {
            var explains = new Hashtable();
            //var list = new List<Hashtable>(arr.Length);
  
            foreach (var item in arr)
            {
                
                if (WordExplorerPresenter.EntityList.ContainsKey(item))
                {
                    var hash=new Hashtable();
                    var word=WordExplorerPresenter.EntityList[item];
                    hash["Comment"]=word.Comment;
                    hash["Pron"]=word.Pron;
                    explains[item] = hash;
                }
            }
            return explains;
        }

        
        public void ReadSelectedWord(string name)
        {
            DefaultReadWordEngine.UseTTSReadWord(name);
        }
    }
}
